package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.*;
import com.gxa.zhonglong.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author asus
 */
@CrossOrigin
@RestController
@RequestMapping("/Goods")
@Api(value = "添加入库商品")
public class GoodsController {

    @Autowired
    GoodsPriceService goodsPriceService = null;

    @Autowired
    GoodsGoldService goodsGoldService = null;

    @Autowired
    GoodsStyleService goodsStyleService = null;
    
    @Autowired
    StoneShapeService stoneShapeService = null;

    @Autowired
    GoodsPropertyService goodsPropertyService = null;

    @Autowired
    GoodsPictureService goodsPictureService = null;

    @Autowired
    GoodsService goodsService = null;

    @Autowired
    private StoneColorService stoneColorService;


    @Autowired
    SupplierService supplierService = null;

    @PostMapping("/putGoods")
    @ApiOperation(value ="添加商品(保存并新建/保存并退出)")
    public Map insertGoods(@RequestBody GoodsAndGoodsPictureVo goodsAndGoodsPictureVo) {
        Goods goods = goodsAndGoodsPictureVo.getGoods();
        System.out.println(goods);
        GoodsPicture goodsPicture = goodsAndGoodsPictureVo.getGoodsPicture();
        goodsService.insertGoods(goods);
        goodsPictureService.insertGoodsPicture(goodsPicture);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

   @PostMapping("/getMap")
   @ApiOperation(value = "(点击添加入库)查询相关表的数据")
    public Map selectAll() throws Exception {
       List<StoneColor> color = stoneColorService.getStoneColor();
       List<Map<String, Object>> property = goodsPropertyService.selectAll();
       List<StoneShape> stone = stoneShapeService.getStoneShapeList();
       List<Map<String, Object>> styleName = goodsStyleService.selectAll();
       List<Map<String, Object>> gold = goodsGoldService.selectAll();
       List<GoodsPrice> priceClass = goodsPriceService.getGoodsPriceList();
       Map<String,List> map = new HashMap<>();
    
       map.put("color",color);
       map.put("property",property);
       map.put("stone",stone);
       map.put("styleName",styleName);
       map.put("gold",gold);
       map.put("priceClass",priceClass);

       JsonResult jsonResult = new JsonResult();
       jsonResult.setData(map);
       jsonResult.setCode("1");
       return jsonResult.getDataMap();
    }



    @PostMapping("/deleteByGoodsNumber")
    @ApiOperation(value = "根据商品编码删除商品记录")
    public Map deleteByGoodsNumber(@RequestParam("number") String number ) throws BusinessException {

        System.out.println(number);
        JsonResult jsonResult = new JsonResult();
         goodsService.deleteByNumber(number);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getAllByPage")
    @ApiOperation(value = "(选择了分页信息后的接口)查询全部进行分页")
    public Map selectAll(@RequestBody Where where){
        List<Map<String, Object>> goodsList = goodsService.selectAll(where);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(goodsList);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getByNumber")
    @ApiOperation(value = "(商品详情)通过商品编码查看商品详情")
    public  Map selectByFixedCodingNumber(@RequestParam("Number") String number){
        List<Map<String,Object>> maps = goodsService.selectByNumberAllFiled(number);
        System.out.println(number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

}

